package snafu;

import snafu.controller.WorldController;
import snafu.framework.agents.AgentItself;
import snafu.framework.objects.WorldObject;
import snafu.framework.worlds.Position;
import snafu.framework.worlds.WorldMap;
import snafu.gui.MainFrame;
import snafu.implementation.agents.GoldSeekingHiker;
import snafu.implementation.agents.RandomWumpus;
import snafu.implementation.objects.caveground.CavegroundItself;
import snafu.implementation.objects.gold.GoldItself;
import snafu.implementation.objects.grass.Grass;
import snafu.implementation.objects.pit.PitItself;
import snafu.implementation.worlds.HexWorldMap;

public class Wumpus {
	public static void main(String[] args) {
   // =================== START EDIT HERE ===================
     // change the map
//		WorldMap worldMap = new SquareWorldMap(10, 10);
		WorldMap worldMap = new HexWorldMap(10, 10);

		int PIT_DENSITY = 5;         // Percentage of pits
		int GOLD_DENSITY = 10;       // Percentage of gold
		int WUMPUS_COUNT = 3;        // number of wumpus
   // =================== END EDIT HERE ===================		
      
      int fields = worldMap.getHeight() * worldMap.getWidth();
      worldMap.fillWith(CavegroundItself.class);
      final WorldController controller = new WorldController(10, worldMap);
      
		// add pits
		int pitCount = (int) (fields * (PIT_DENSITY / 100f));
		for (int i = 0; i < pitCount; i++) {
			int rX = (int) (Math.random() * worldMap.getWidth());
			int rY = (int) (Math.random() * worldMap.getHeight());
			Position pos = new Position(rX, rY);
			
			// remove grass from this place
			WorldObject grass = worldMap.getObject(pos, Grass.class);
			worldMap.removeObject(grass);
			
			controller.addObject(pos, new PitItself());
		}
		
		// add gold
		int goldCount = (int) (fields * (GOLD_DENSITY / 100f));
		for (int i = 0; i < goldCount; i++) {
			boolean foundPlace = false;
			int rX = 0;
			int rY = 0;
			while (!foundPlace) {
				rX = (int) (Math.random() * worldMap.getWidth());
				rY = (int) (Math.random() * worldMap.getHeight());
				
				// don't place gold on pits
				foundPlace = true;
				for (WorldObject object : worldMap.get(new Position(rX, rY)).getWorldObjects()) {
					if (object instanceof PitItself) {
						foundPlace = false;
					}
				}
			}
			controller.addObject(new Position(rX, rY), new GoldItself());
		}

		// add wumpus
		for (int i = 0; i < WUMPUS_COUNT; i++) {
			int rX = (int) (Math.random() * worldMap.getWidth());
			int rY = (int) (Math.random() * worldMap.getHeight());
			controller.addObject(new Position(rX, rY), new AgentItself(new RandomWumpus()));
		}
		
		// add agent
		int rX = (int) (Math.random() * worldMap.getWidth());
		int rY = (int) (Math.random() * worldMap.getHeight());
		controller.addObject(new Position(rX, rY), new AgentItself(new GoldSeekingHiker()));
		
		// GUI
		new MainFrame(controller);
	}
}
